{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python map 函数\n",
    "\n",
    "学习参考地址：https://blog.csdn.net/qq_42778001/article/details/101436742\n",
    "\n",
    "功能：\n",
    "\n",
    "* 应用一个函数到输入列表的所有项\n",
    "\n",
    "蓝图（blueprint）：\n",
    "\n",
    "```script\n",
    "map(function, list_of_inputs)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 传统方法\n",
    "\n",
    "产生5个元素的随机整数数列（列表）；基于这个随机数列产生一个新数列，新数列是原数列各元素值乘以2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "lst1 = [random.randint(1, 99) for x in range(5)]\n",
      " [44, 20, 99, 99, 44]\n",
      "lst2\n",
      " [88, 40, 198, 198, 88]\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "\n",
    "# 生成一个随机整数数列(列表)\n",
    "lst1 = [random.randint(1, 99) for x in range(5)]\n",
    "print(\"lst1 = [random.randint(1, 99) for x in range(5)]\\n\", lst1)\n",
    "\n",
    "# 循环生成lst2\n",
    "lst2 = []\n",
    "for x in lst1:\n",
    "    lst2.append(x * 2)\n",
    "    \n",
    "print(\"lst2\\n\", lst2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### map方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "lst1 = [random.randint(1, 99) for x in range(5)]\n",
      " [26, 63, 20, 81, 88]\n",
      "lst2 = list(map(myfunc, lst1))\n",
      " [52, 126, 40, 162, 176]\n"
     ]
    }
   ],
   "source": [
    "# import random\n",
    "\n",
    "# 自定义方法，在map函数中调用\n",
    "def myfunc(x):\n",
    "    return x * 2\n",
    "\n",
    "# 生成一个随机整数数列(列表)\n",
    "lst1 = [random.randint(1, 99) for x in range(5)]\n",
    "print(\"lst1 = [random.randint(1, 99) for x in range(5)]\\n\", lst1)\n",
    "\n",
    "# 使用自定义函数myfunc\n",
    "lst2 = list(map(myfunc, lst1))\n",
    "print(\"lst2 = list(map(myfunc, lst1))\\n\", lst2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### map and lambda"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "lst1 = [random.randint(1, 99) for i in range(5)]\n",
      " [63, 88, 61, 15, 58]\n",
      "lst2 = list(map(lambda x: x * 2, lst1))\n",
      " [126, 176, 122, 30, 116]\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "\n",
    "# 生成一个随机整数数列(列表)\n",
    "lst1 = [random.randint(1, 99) for i in range(5)]\n",
    "print(\"lst1 = [random.randint(1, 99) for i in range(5)]\\n\", lst1)\n",
    "\n",
    "# map + lambda\n",
    "lst2 = list(map(lambda x: x * 2, lst1))\n",
    "print(\"lst2 = list(map(lambda x: x * 2, lst1))\\n\", lst2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python lambda 函数\n",
    "\n",
    "* 它是简单的一行函数\n",
    "* 它不使用def也不使用return关键字\n",
    "\n",
    "### 参看下面示例：\n",
    "\n",
    "```python\n",
    "lambda x: x * 2\n",
    "```\n",
    "\n",
    "对于上面lambda函数，它可以写为下面普通形式：\n",
    "\n",
    "```python\n",
    "def double(x):\n",
    "    return x * 2\n",
    "```\n",
    "\n",
    "### lambda 蓝图：\n",
    "\n",
    "```script\n",
    "lambda <parameter(s)>: expression\n",
    "```\n",
    "\n",
    "### 再看下面lambda函数的例子：\n",
    "\n",
    "```python\n",
    "lambda x, y: x + y\n",
    "```\n",
    "\n",
    "上面例子的普通函数形式为：\n",
    "\n",
    "```python\n",
    "def add(x, y):\n",
    "    return x + y\n",
    "```\n",
    "\n",
    "### lambda 三元操作符\n",
    "\n",
    "```python\n",
    "vmax = lambda x, y: x if x > y else y\n",
    "```\n",
    "\n",
    "上面例子的普通函数形式为：\n",
    "\n",
    "```python\n",
    "def getMax(x, y):\n",
    "    if x > y:\n",
    "        return x\n",
    "    else:\n",
    "        return y\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python filter 函数\n",
    "\n",
    "filter函数支持以下功能：\n",
    "\n",
    "* 从一个序列中过滤相关的数据项\n",
    "* 返回一个过滤后的列表（list）\n",
    "\n",
    "蓝图：\n",
    "\n",
    "```script\n",
    "filter(function, list_of_inputs)\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "lst1 = [random.randint(1, 99) for i in range(10)]\n",
      " [81, 4, 10, 65, 65, 32, 67, 53, 69, 69]\n",
      "list(filter(lambda x: x > 50, lst1))\n",
      " [81, 65, 65, 67, 53, 69, 69]\n",
      "<class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# filter lambda\n",
    "import random\n",
    "\n",
    "lst1 = [random.randint(1, 99) for i in range(10)]\n",
    "print(\"lst1 = [random.randint(1, 99) for i in range(10)]\\n\", lst1)\n",
    "\n",
    "lst2 = list(filter(lambda x: x > 50, lst1))\n",
    "print(\"list(filter(lambda x: x > 50, lst1))\\n\", lst2)\n",
    "print(type(lst2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "lst1 = [random.randint(1, 99) for i in range(10)]\n",
      " [42, 81, 87, 87, 26, 34, 52, 19, 64, 6]\n",
      "lst2 = gt50(lst1)\n",
      " [81, 87, 87, 52, 64]\n"
     ]
    }
   ],
   "source": [
    "# 过滤的普通函数形式\n",
    "import random\n",
    "\n",
    "lst1 = [random.randint(1, 99) for i in range(10)]\n",
    "print(\"lst1 = [random.randint(1, 99) for i in range(10)]\\n\", lst1)\n",
    "\n",
    "def gt50(lst):\n",
    "    rst = [x for x in lst if x > 50]\n",
    "    return rst\n",
    "\n",
    "lst2 = gt50(lst1)\n",
    "print(\"lst2 = gt50(lst1)\\n\", lst2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python array\n",
    "\n",
    "* 用于创建指定数据类型的数组，请参看下面的蓝图：\n",
    "\n",
    "```script\n",
    "from array import array\n",
    "array('<type>', <list>)\n",
    "```\n",
    "\n",
    "Python array支持的类型：\n",
    "\n",
    "* b, signed char\n",
    "* B, unsigned char\n",
    "* d, double\n",
    "* f, float\n",
    "* u, python unicode\n",
    "* i, signed int\n",
    "* I, unsigned int\n",
    "* h, signed short\n",
    "* H, unsigned short\n",
    "* l, signed long\n",
    "* L, unsigned long\n",
    "\n",
    "导入：\n",
    "\n",
    "```python\n",
    "from array import array\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "array('i', [0, 1, 2, 3, 4])\n"
     ]
    }
   ],
   "source": [
    "from array import array\n",
    "\n",
    "a = array(\"i\", list(range(5)))\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "demo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n",
      "type(r) =  <class 'requests.models.Response'>\n",
      "r.headers\n",
      " {'Cache-Control': 'private, no-cache, no-store, proxy-revalidate, no-transform', 'Connection': 'keep-alive', 'Content-Encoding': 'gzip', 'Content-Type': 'text/html', 'Date': 'Wed, 12 Jan 2022 02:51:58 GMT', 'Last-Modified': 'Mon, 23 Jan 2017 13:27:56 GMT', 'Pragma': 'no-cache', 'Server': 'bfe/1.0.8.18', 'Set-Cookie': 'BDORZ=27315; max-age=86400; domain=.baidu.com; path=/', 'Transfer-Encoding': 'chunked'}\n",
      "ISO-8859-1\n",
      "<!DOCTYPE html>\n",
      "<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下，你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class=\"bg s_ipt_wr\"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class=\"bg s_btn_wr\"><input type=submit id=su value=百度一下 class=\"bg s_btn\"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href=\"http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === \"\" ? \"?\" : \"&\")+ \"bdorz_come=1\")+ '\" name=\"tj_login\" class=\"lb\">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style=\"display: block;\">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "\n",
    "r = requests.get(\"http://www.baidu.com\")\n",
    "print(r.status_code)    # 200 OK\n",
    "print(\"type(r) = \", type(r))\n",
    "print(\"r.headers\\n\", r.headers)\n",
    "# print(r.text)\n",
    "print(r.encoding)\n",
    "r.encoding = 'utf-8'\n",
    "print(r.text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<!DOCTYPE html>\n",
      "<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css><title>百度一下，你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class=\"bg s_ipt_wr\"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus=autofocus></span><span class=\"bg s_btn_wr\"><input type=submit id=su value=百度一下 class=\"bg s_btn\" autofocus></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=https://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href=\"http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === \"\" ? \"?\" : \"&\")+ \"bdorz_come=1\")+ '\" name=\"tj_login\" class=\"lb\">登录</a>');\n",
      "                </script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style=\"display: block;\">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 爬取网页代码范式\n",
    "import requests\n",
    "\n",
    "def getHTMLContent(url):\n",
    "    try:\n",
    "        r = requests.get(url, timeout=10)\n",
    "        r.raise_for_status() # 如果状态不是200则引发异常\n",
    "        r.encoding = r.apparent_encoding\n",
    "        return r.text\n",
    "    except:\n",
    "        return \"异常\"\n",
    "    \n",
    "if __name__ == \"__main__\":\n",
    "    url = \"https://www.baidu.com\"\n",
    "    print(getHTMLContent(url))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = {}\n",
    "a[\"name\"] = \"Jerry\"\n",
    "print(type(a))\n",
    "print(a)\n",
    "b = {1, 2, 3}\n",
    "print(type(b))\n",
    "c = set()\n",
    "print(type(c))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "\n",
    "a = [x for x in range(10)]\n",
    "print(a)\n",
    "\n",
    "b = [random.randint(1, 9) * x for x in range(10)]\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4]\n",
      "[0, 1, 2, 3, 4, 'H', 'e', 'l', 'l', 'o']\n",
      "[0, 1, 2, 3, 4, 'H', 'e', 'l', 'l', 'o', 'H', 'e', 'l', 'l', 'o']\n"
     ]
    }
   ],
   "source": [
    "nums = list(range(5))\n",
    "print(nums)     # [0, 1, 2, 3, 4]\n",
    "nums = [*range(5), *\"Hello\"]\n",
    "print(nums)     # [0, 1, 2, 3, 4, 'H', 'e', 'l', 'l', 'o']\n",
    "\n",
    "a = [*nums, *\"Hello\"]\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'gender': 'female', 'name': 'tom'}\n",
      "name= tom \n",
      "gender= female\n",
      "ddd\n",
      "name= Jerry \n",
      "gender= male\n"
     ]
    }
   ],
   "source": [
    "d = {\n",
    "    \"gender\": \"female\",\n",
    "    \"name\": \"tom\"\n",
    "}\n",
    "\n",
    "print(d)\n",
    "\n",
    "def getDict(name = \"Jerry\", gender=\"male\"):\n",
    "    print(\"name=\",name,\"\\ngender=\",gender)\n",
    "\n",
    "getDict(**d)\n",
    "print(\"ddd\")\n",
    "getDict()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "def <function name>([para list]):\n",
    "    <code block>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'abcd' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_10316/545624037.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      6\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m \u001b[0msayHello\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_10316/545624037.py\u001b[0m in \u001b[0;36msayHello\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0msayHello\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m     \u001b[0mabcd\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"hello\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m     \u001b[1;32mpass\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'abcd' is not defined"
     ]
    }
   ],
   "source": [
    "def sayHello():\n",
    "    abcd()\n",
    "    print(\"hello\")\n",
    "    pass\n",
    "\n",
    "    \n",
    "sayHello()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "number is 2, by is 1\n",
      "3\n",
      "number is 3, by is 1\n",
      "4\n",
      "number is 1, by is 4\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "def increment(number, by):\n",
    "    print(f\"number is {number}, by is {by}\")\n",
    "    return number + by\n",
    "\n",
    "\n",
    "print(increment(2, 1))\n",
    "print(increment(number=3, by=1))\n",
    "print(increment(by=4, number=1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "504\n"
     ]
    }
   ],
   "source": [
    "def multiply(*numbers):\n",
    "    total = 1\n",
    "    for n in numbers:\n",
    "        total *= n\n",
    "    return total\n",
    "\n",
    "\n",
    "# print(multiply(1, 2, 3, 4, 5, 6))\n",
    "\n",
    "a = [7, 8, 9]\n",
    "print(multiply(*a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'id': 1, 'name': 'John', 'age': 22}\n",
      "1\n",
      "John\n"
     ]
    }
   ],
   "source": [
    "def saveUser(**user):\n",
    "    return user\n",
    "\n",
    "\n",
    "usr = saveUser(id=1, name=\"John\", age=22)\n",
    "\n",
    "print(usr)\n",
    "print(usr[\"id\"])\n",
    "print(usr[\"name\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<Response [403]>\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "\n",
    "kv = {\n",
    "    'key1': 'value1',\n",
    "    'key2': 'value2'\n",
    "}\n",
    "\n",
    "r = requests.request('POST', 'http://python123.io/ws', data=kv)\n",
    "\n",
    "body=\"body content\"\n",
    "r = requests.request('POST', 'http://python123.io/ws', data=body)\n",
    "print(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<Response [403]>\n"
     ]
    }
   ],
   "source": [
    "kv = {\n",
    "    \"key1\": \"value1\"\n",
    "}\n",
    "\n",
    "r = requests.request(\"POST\", \"http://python123.io/ws\", json=kv)\n",
    "print(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<Response [403]>\n"
     ]
    }
   ],
   "source": [
    "hd = {\n",
    "    \"user-agent\": \"Chrome/10\"\n",
    "}\n",
    "\n",
    "r = requests.request(\"POST\", \"http://python123.io/ws\", headers=hd)\n",
    "print(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fs = {\n",
    "    \"file\": open(\"data.xls\", \"rb\")\n",
    "}\n",
    "\n",
    "r = requests.request(\"POST\", \"http://python123.io/ws\", files=fs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.request(\"POST\", \"http://python123.io/ws\", timeout=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pxs = {\n",
    "    \"http\": \"http://user:pass@10.19.19.1:1234\",\n",
    "    \"https\": \"https://10.10.10.1:4321\"\n",
    "}\n",
    "\n",
    "r = requests.request(\"GET\", \"http://www.baidu.com\", proxies=yes)"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "d80dceb8e76061e928794bb3ce8fa9d0c2b7abe9c550ab61d3fca5a520d3bcb0"
  },
  "kernelspec": {
   "display_name": "Python 3.8.6 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
